
$(function() {
		
	$("input[id^='code_s']:visible").autocomplete('contact.php?do=Express.ajax_code', {
		width: 200,
		max: 20,
		dataType: "json",
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.name,
					result: row.code
				}
			});
		},
		formatItem: function(item) {
			return item.code + " [" + item.name + ", " + item.category + "]";
		}
	});

	$("input[id^='name_s']:visible").autocomplete('contact.php?do=Express.ajax_zjm', {
		width: 200,
		max: 20,
		dataType: "json",
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.name,
					result: row.name
				}
			});
		},
		formatItem: function(item) {
			return item.zjm + " [" + item.name + ", " + item.category + "]";
		}
	});	
	
	Tip.emp('负责人');
});

var Express = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}

				if (!Contact.validate_add()) {
					return false;
				}

				var linkman = $("input[name='linkman']");
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				var mobile = $("input[name='mobile']");
				var email = $("input[name='email']");
				var content = $("input[name='content']");

				var aa = new Array();
				for (var i = 0; i < linkman.length; i++) {
					var contact = {};
					contact.linkman = $(linkman[i]).val();				
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					contact.mobile = $(mobile[i]).val();
					contact.email = $(email[i]).val();
					contact.content = $(content[i]).val();
					
					aa.push(contact);
				}

				var form = $("#form_add")[0];

				var body = { };
				body.name = form.name.value;			
				body.fullname = form.fullname.value;
				
				body.category = form.category.value;

				body.address = form.address.value;
				body.url = form.url.value;
				body.jj = form.jj.value;
				body.content = form.content.value;

				body.contact = $.toJSON(aa);
				
				$(this).dialog("close");

				$.post('contact.php?do=Express.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
		
		dialog_add(url, title, xy);
	},

	clean_cache: function () {
		$.getJSON('contact.php?do=Express.audit&item=52', function(json) {
			if (json.status == 1) {
				if (!confirm("确认是否清除缓存？")) {
					return false;
				}

				$.get('contact.php?do=Express.ajax_category_type', function(data) {
					alert("操作成功！");
				});
			} else {			
				_error();
			}
		});
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);
		
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'contact.php?do=Express.edit&id=' + id;	
		var c2 = 'contact.php?do=Express.view&id=' + id;

		var c3 = 'info.php?do=Contact.add&info=4&id=' + id;
		var c4 = 'finance.php?do=Bank.supplier&id=' + id;

		var a1 = ['编辑信息', 'url', 'Express.dialog_edit_', '650x450'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x420'];

		var a3 = ['联系信息', 'url', 'Express.dialog_add_contact', '500x300'];
		var a4 = ['银行帐号', 'url', 'Express.dialog_bank', '650x450'];
		
		var s = c1 + ", " + c2 + ", " + c4;

		return popup(e, [a1, a2, a4], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('contact.php?do=Express.audit', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						if (!ContactEdit.validate_edit()) {
							return false;
						}
			
						var id = $("input[name='id']");
						var linkman = $("input[name='linkman']");
						var tel = $("input[name='tel']");
						var fax = $("input[name='fax']");
						var mobile = $("input[name='mobile']");
						var email = $("input[name='email']");
						var content = $("input[name='content']");
			
						var aa = new Array();
						for (var i = 0; i < z.length; i++) {
							var contact = {};
							var b = $("#form" + z[i] + " :input[name='id']");
							if (b != null) {
								contact.id = $("#form" + z[i] + " :input[name='id']").val();
							}
			
							contact.linkman = $(linkman[i]).val();				
							contact.tel = $(tel[i]).val();
							contact.fax = $(fax[i]).val();
							contact.mobile = $(mobile[i]).val();
							contact.email = $(email[i]).val();
							contact.content = $(content[i]).val();
							
							aa.push(contact);
						}
			
						var form = $("#form_edit")[0];
			
						var star = $("input[name='star']:checked");
			
						var body = { };
						body.id = form.info_id.value;
						body.name = form.name.value;
						body.fullname = form.fullname.value;
						body.star = star.val();
			
						body.category = form.category.value;
									
						body.address = form.address.value;
						body.url = form.url.value;
						body.jj = form.jj.value;
						body.content = form.content.value;
						body.status = form.status.value;
			
						body.contact = $.toJSON(aa);
			
						body.z = $.cookie('contact');
			
						$.cookie('contact', null);
			
						$(this).dialog("close");
						
						var url = 'contact.php?do=Express.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_bank: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {			
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
							
				var form = $("#form_add")[0];
	
				var body = { };
				body.id = form.id.value;
				body.bank = form.bank.value;
				body.branch = form.branch.value;
	
				body.province = form.province.value;
				body.city = form.city.value;
				
				body.zone = form.zone.value;
				body.type = form.type.value;
				
				body.name = form.name.value;
				body.code = form.code.value;
				
				body.content = form.content.value;
	
				$.post('finance.php?do=Bank.supplierPost', body, function(data) {
					$('#dialog_add').html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	}
};
